/*
 * LZXStubsFrame.java July 2009
 *
 * Copyright (C) 2009, Quirino Zagarese <quirino@laszloitalia.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 */
package org.eu4ria.tools.lzstubs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.eu4ria.tools.LZStubs;

public class LZXStubsFrame extends JFrame {

	private JFileChooser jarFileChooser;

	private JFileChooser outputFileChooser;

	private static LZXStubsFrame instance = null;

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jarPathLabel = null;
	private JTextField outputPathField = null;
	private JLabel outputPathLabel = null;
	private JTextField jarPathField = null;
	private JPanel topPanel = null;
	private JPanel jPanel = null;
	private JPanel jPanel1 = null;
	private JButton jButton = null;
	private JButton jButton1 = null;

	private JPanel centerPanel = null;

	private JLabel jLabel = null;

	private JTextField jTextField = null;

	private JLabel jLabel1 = null;

	private JTextField jTextField1 = null;

	private JLabel jLabel2 = null;

	private JTextField jTextField2 = null;

	private JLabel jLabel31 = null;

	private JCheckBox jCheckBox = null;

	private JPanel jPanel2 = null;

	private JButton jButton2 = null;

	/**
	 * This is the default constructor
	 */

	public static LZXStubsFrame getInstance() {
		if (instance == null) {
			instance = new LZXStubsFrame();
		}
		return instance;
	}

	private LZXStubsFrame() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	private void initialize() {
		this.setSize(663, 500);
		this.setContentPane(getJContentPane());
		this.setTitle("EU4RIA  LZX stub generator");
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				LZStubs.saveProperties();
			}
		});
	}

	/**
	 * This method initializes jContentPane
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			outputPathLabel = new JLabel();
			outputPathLabel.setText("Output folder:");
			outputPathLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
			jarPathLabel = new JLabel();
			jarPathLabel.setText("Your application jar file:");
			jarPathLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.setBackground(Color.white);
			jContentPane.add(getTopPanel(), BorderLayout.NORTH);
			jContentPane.add(getCenterPanel(), BorderLayout.CENTER);
			jContentPane.add(getJPanel2(), BorderLayout.SOUTH);
			jarFileChooser = new JFileChooser();
			jarFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
			outputFileChooser = new JFileChooser();
			outputFileChooser
					.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextField
	 *
	 * @return javax.swing.JTextField
	 */
	public JTextField getOutputPathField() {
		if (outputPathField == null) {
			outputPathField = new JTextField();
			outputPathField.setEditable(false);
		}
		return outputPathField;
	}

	/**
	 * This method initializes jTextField1
	 *
	 * @return javax.swing.JTextField
	 */
	public JTextField getJarPathField() {
		if (jarPathField == null) {
			jarPathField = new JTextField();
			jarPathField.setPreferredSize(new Dimension(200, 19));
			jarPathField.setEditable(false);
		}
		return jarPathField;
	}

	/**
	 * This method initializes jPanel
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getTopPanel() {
		if (topPanel == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(2);
			gridLayout.setColumns(1);
			topPanel = new JPanel();
			topPanel.setPreferredSize(new Dimension(600, 60));
			topPanel.setLayout(gridLayout);
			topPanel.setBackground(Color.white);
			topPanel.add(getJPanel(), null);
			topPanel.add(getJPanel1(), null);
		}
		return topPanel;
	}

	/**
	 * This method initializes jPanel
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridLayout gridLayout1 = new GridLayout();
			gridLayout1.setRows(1);
			gridLayout1.setColumns(3);
			jPanel = new JPanel();
			jPanel.setLayout(gridLayout1);
			jPanel.add(jarPathLabel, null);
			jPanel.add(getJarPathField(), null);
			jPanel.add(getJButton(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridLayout gridLayout2 = new GridLayout();
			gridLayout2.setRows(1);
			gridLayout2.setColumns(3);
			jPanel1 = new JPanel();
			jPanel1.setLayout(gridLayout2);
			jPanel1.add(outputPathLabel, null);
			jPanel1.add(getOutputPathField(), null);
			jPanel1.add(getJButton1(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jButton
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Browse");
			jButton
					.setToolTipText("Browse for the jar archive which contains your application");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int returnVal = jarFileChooser
							.showOpenDialog(LZXStubsFrame.this);
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						File file = jarFileChooser.getSelectedFile();
						LZXStubsFrame.getInstance().getJarPathField().setText(
								file.getAbsolutePath());
					} else {

					}

				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Browse");
			jButton1
					.setToolTipText("Select the folder where the stubs.lzx will be placed");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int returnVal = outputFileChooser
							.showOpenDialog(LZXStubsFrame.this);
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						File file = outputFileChooser.getSelectedFile();
						LZXStubsFrame.getInstance().getOutputPathField()
								.setText(file.getAbsolutePath());
					} else {

					}
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jPanel2
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getCenterPanel() {
		if (centerPanel == null) {
			jLabel31 = new JLabel();
			jLabel31.setText("Generate stubtest file:");
			jLabel31.setFont(new Font("Dialog", Font.PLAIN, 12));
			jLabel2 = new JLabel();
			jLabel2.setText("Timeout interval(ms):");
			jLabel2.setFont(new Font("Dialog", Font.PLAIN, 12));
			jLabel1 = new JLabel();
			jLabel1.setText("Port:");
			jLabel1.setFont(new Font("Dialog", Font.PLAIN, 12));
			jLabel = new JLabel();
			jLabel.setText("Host:");
			jLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
			GridLayout gridLayout3 = new GridLayout();
			gridLayout3.setRows(4);
			gridLayout3.setColumns(2);
			centerPanel = new JPanel();
			centerPanel.setBackground(new Color(238, 238, 238));
			centerPanel.setLayout(gridLayout3);
			centerPanel.add(jLabel, null);
			centerPanel.add(getHostField(), null);
			centerPanel.add(jLabel1, null);
			centerPanel.add(getPortField(), null);
			centerPanel.add(jLabel2, null);
			centerPanel.add(getTimeoutField(), null);
			centerPanel.add(jLabel31, null);
			centerPanel.add(getStubTestCheckBox(), null);

		}
		return centerPanel;
	}

	/**
	 * This method initializes jTextField
	 *
	 * @return javax.swing.JTextField
	 */
	public JTextField getHostField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setText("localhost");
			jTextField
					.setToolTipText("Insert the address of the application server hosting a EU4RIA instance (i.e. www.mysite.com or 192.168.1.43)");
		}
		return jTextField;
	}

	/**
	 * This method initializes jTextField1
	 *
	 * @return javax.swing.JTextField
	 */
	public JTextField getPortField() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setText("8080");
			jTextField1
					.setToolTipText("Insert the port number your application server is listening to (Tomcat default is 8080)");
		}
		return jTextField1;
	}

	/**
	 * This method initializes jTextField2
	 *
	 * @return javax.swing.JTextField
	 */
	public JTextField getTimeoutField() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField();
			jTextField2.setText("10000");
			jTextField2
					.setToolTipText("Insert requests timeout interval (10000 means 10 seconds) ");
		}
		return jTextField2;
	}

	/**
	 * This method initializes jCheckBox
	 *
	 * @return javax.swing.JCheckBox
	 */
	public JCheckBox getStubTestCheckBox() {
		if (jCheckBox == null) {
			jCheckBox = new JCheckBox();
			jCheckBox
					.setToolTipText("Check it if you want a stubtest.lzx file to be generated. This way you can test client-server communication before completing your Laszlo application.");
			jCheckBox.setEnabled(true);
		}
		return jCheckBox;
	}

	/**
	 * This method initializes jPanel2
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			jPanel2 = new JPanel();
			jPanel2.setLayout(new FlowLayout());
			jPanel2.add(getJButton2(), null);
		}
		return jPanel2;
	}

	/**
	 * This method initializes jButton2
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setText("Generate");
			jButton2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					LZXStubsFrame frame = LZXStubsFrame.getInstance();
					File input = new File(frame.getJarPathField().getText());
					File outputFolder = new File(frame.getOutputPathField()
							.getText());
					String host = frame.getHostField().getText();
					int port = Integer.parseInt(frame.getPortField().getText());
					int timeout = Integer.parseInt(frame.getTimeoutField()
							.getText());
					boolean createStubTest = frame.getStubTestCheckBox()
							.isSelected();
					try {
						LZStubs.createStubs(input, outputFolder, host, port,
								timeout, createStubTest);
						JOptionPane.showMessageDialog(null, "Stubs generated!",
								"", JOptionPane.INFORMATION_MESSAGE);
					} catch (Exception e1) {
						JOptionPane.showMessageDialog(null, "Error: "
								+ e1.getMessage(), "Error",
								JOptionPane.ERROR_MESSAGE);
						e1.printStackTrace();
					}
				}
			});
		}
		return jButton2;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
